﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class AttendanceCertificateDAO : IDAO<AttendanceCertificate>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(AttendanceCertificate entity)
        {
            agrDb.AttendanceCertificates.AddObject(entity);
        }

        public void remove(AttendanceCertificate entity)
        {
            agrDb.AttendanceCertificates.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (AttendanceCertificate attendanceCertificate in agrDb.AttendanceCertificates.Where(s => s.CertificateCode == code))
            {
                agrDb.AttendanceCertificates.DeleteObject(attendanceCertificate);
            }
        }

        public void merge(AttendanceCertificate entity)
        {
            foreach (AttendanceCertificate attendanceCertificate in agrDb.AttendanceCertificates.Where(s => s.CertificateCode == entity.CertificateCode))
            {
                attendanceCertificate.CertificateDescription = entity.CertificateDescription;
                attendanceCertificate.CertificateType = entity.CertificateType;
            }

            //Select ALL
            //var attendanceCertificates = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public AttendanceCertificate findById(string code)
        {
            AttendanceCertificate attendanceCertificate = new AttendanceCertificate();

            foreach (AttendanceCertificate cert in agrDb.AttendanceCertificates.Where(s => s.CertificateCode == code))
            {
                attendanceCertificate = cert;
            }

            return attendanceCertificate;
        }

        public IEnumerable<AttendanceCertificate> findAll()
        {
            var attendanceCertificates = (from cert in agrDb.AttendanceCertificates
                            select cert);

            return attendanceCertificates;
        }

        public IEnumerable<AttendanceCertificate> findInRange(int firstResult, int maxResuslts)
        {
            var attendanceCertificates = (from cert in agrDb.AttendanceCertificates
                            select cert);

            return attendanceCertificates.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var attendanceCertificates = (from cert in agrDb.AttendanceCertificates
                            select cert).ToList();

            return attendanceCertificates.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}